package su.myspringapps.onlineschool.entity;

import io.hypersistence.utils.hibernate.type.basic.PostgreSQLEnumType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import su.myspringapps.onlineschool.entity.enums.CourseType;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

/**
 * Класс-сущность Course
 * <p/>
 * Описывает курс
 * <p/>
 *
 * @author petr.mikhailin
 * created 20.05.2024 9:41
 */
@Entity
@Table(name = "courses")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@TypeDef(name = "pgsql_enum", typeClass = PostgreSQLEnumType.class)
public class Course {

    @Id
    @GeneratedValue(generator = "course-sequence-generator")
    @GenericGenerator(
            name = "course-sequence-generator",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                    @Parameter(name = "sequence_name", value = "courses_id_seq")
            }
    )
    private Integer id;

    private String name;

    private Integer duration;

    @Type(type = "pgsql_enum")
    @Enumerated(EnumType.STRING)
    private CourseType type;

    private String description;

    @ManyToOne
    private Teacher teacher;

    @Column(name = "students_count")
    private Integer studentsCount;

    private Integer price;

    @Column(name = "price_per_hour")
    private Double pricePerHour;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
    private List<Subscription> subscriptions;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Course course = (Course) o;

        if (!Objects.equals(id, course.id)) return false;
        if (!Objects.equals(name, course.name)) return false;
        if (!Objects.equals(duration, course.duration)) return false;
        if (type != course.type) return false;
        if (!Objects.equals(description, course.description)) return false;
        if (!Objects.equals(studentsCount, course.studentsCount))
            return false;
        if (!Objects.equals(price, course.price)) return false;
        return Objects.equals(pricePerHour, course.pricePerHour);
    }
}
